/************************************************************************************
 *                                .                                                 *
 *                                .                                                 *
 ************************************************************************************/
package jMiiEditor.mii;

import jMiiEditor.mii.introspection.MiiGetterIntrospection;
import jMiiEditor.mii.introspection.MiiGetterIntrospectionItem;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Random;
import cx.ath.choisnet.lang.introspection.IntrospectionInvokeException;

/**
 * Build some Mii object for TestCase :p
 * 
 * @author CC
 */
public class MiiTestHelper 
{   
    public static final Mii getMinMii()
    {
        return new AbtractTestMiiBuilder() {

            @Override
            public byte getByte()
            {
                return 0;
            }
            
        }.getMii();
    }
    
    public static final Mii getMaxMiiFilledWith0xFF()
    {
        return new AbtractTestMiiBuilder() {

            @Override
            public byte getByte()
            {
                return (byte)0xFF;
            }
            
        }.getMii();
    }
    
    public static final Mii getMaxMiiAccordingToConstraits() 
        throws IntrospectionInvokeException
    {
        MiiGetterIntrospection mgi = new MiiGetterIntrospection();
        Mii                 mii = getMaxMiiFilledWith0xFF();
        
        for( Entry<String, MiiGetterIntrospectionItem> entry : mgi.getMap().entrySet() ) {
            //String                  beanName = entry.getKey();
            MiiGetterIntrospectionItem item     = entry.getValue();            
            //MIIGETTER               anno     = item.getAnnotation();
            MIIGETTER.Type          type     = item.getType();
            
            if( type == MIIGETTER.Type.INT ) {
                int intMaxValue = Integer.class.cast( item.getMaxValue() );
                
                if( intMaxValue > 0 ) {
                    item.setIntegerValue( mii, intMaxValue-1 );
                }
            }
        }
        
        return mii;
    }
    
    public static final Mii getRandomMii()
    {
        final Random ran = new Random();
        final byte[] b   = new byte[ 1 ];
        
        return new AbtractTestMiiBuilder() {

            @Override
            public byte getByte()
            {
                ran.nextBytes( b );
                return b[ 0 ];
            }
            
        }.getMii();
    }
    
    abstract static class AbtractTestMiiBuilder
    {
        public abstract byte getByte();

        public Mii getMii()
        {
            try {
                return new Mii( new InputStream() {

                    @Override
                    public int read() throws IOException
                    {
                        return 0x00FF & getByte();
                    }

                } );
            }
            catch( IOException e ) {
                throw new RuntimeException( e );
            }
        }
    }
}